package aburakc.smsweb.server;

import java.util.Date;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;


import aburakc.smsweb.client.SmsService;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SmsServiceImpl extends RemoteServiceServlet implements SmsService {

	 /**
	 * 
	 */
	private static final long serialVersionUID = -7121107804252704023L;
	private static final PersistenceManagerFactory PMF =
	      JDOHelper.getPersistenceManagerFactory("transactions-optional");
	@Override
	public void removeSms(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addSms(String address, String message) {
		long time = System.currentTimeMillis();
		Date date = new Date(time);
		
		PersistenceManager pm = getPersistenceManager();
		Sms sms = new Sms(address, message, date, time);
		Key key = KeyFactory.createKey(Sms.class.getSimpleName(), time);
		sms.setKey(key);
		
		pm.makePersistent(key);
		
		pm.close();
		
		
	}

	@Override
	public Sms getFirstUnsentMessages() {
		// TODO Auto-generated method stub
		return null;
	}

	private PersistenceManager getPersistenceManager() {
		    return PMF.getPersistenceManager();
		  }
}
